package com.bgs.travelapp.card.service.impl;

import com.bgs.travelapp.card.entity.CardEntity;
import com.bgs.travelapp.card.mapper.CardMapper;
import com.bgs.travelapp.card.service.CardService;
import com.bgs.travelapp.fileManager.service.FileManagerService;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.smartcardio.Card;
import java.util.List;

/**
 * 会员卡管理 (后端、前端共用)
 * gpd
 * 2022-07-20 21点10分
 */
@Service
public class CardServiceImpl implements CardService {

    @Resource
    CardMapper cardMapper;

    @Resource
    FileManagerService fileManagerService;

    /**
     * 会员卡新增 （后端--会员卡类型）
     * gpd 2022-08-02 17:09
     * @param cardEntity
     * @return
     */
    @Override
    public Integer saveCard(MultipartRequest multipartRequest, CardEntity cardEntity) {
        MultipartFile file = multipartRequest.getFile("file");
        Integer fileId = 0;
        if (file != null) {
            fileId = fileManagerService.uploadPicture(file);
        }
        cardEntity.setCoverId(fileId);
        cardEntity.setDelFlag(0);
        return cardMapper.insertSelective(cardEntity);
    }

    /**
     * 新增校验方法---查询cardName是否重复（会员卡名称） （后端--会员卡类型）
     * gpd 2022-08-02 17:09
     * @param cardName
     * @return
     */
    @Override
    public List<CardEntity> selectCardName(String cardName) {
        CardEntity cardEntity = new CardEntity();
        cardEntity.setCardName(cardName);
        cardEntity.setDelFlag(0);
        return cardMapper.select(cardEntity);
    }

    /**
     * 分页查询+模糊查（会员卡名称） （后端--会员卡类型）
     * gpd 2022-08-02 17:09
     * @param pageRequest
     * @param cardName
     * @return
     */
    @Override
    public PageResult getCardByPage(PageRequest pageRequest, String cardName) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(CardEntity.class);
        example.createCriteria().andLike("cardName", "%" + cardName + "%").andEqualTo("delFlag", 0);
        PageResult pageResult = MyPageHelper.pageInfo(cardMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 修改会员卡 （后端--会员卡类型）
     * gpd 2022-08-02 17:09
     * @param cardEntity
     * @return
     */
    @Override
    public Integer updateCard(MultipartRequest multipartRequest, CardEntity cardEntity) {
        MultipartFile file = multipartRequest.getFile("file");
        Integer coverId = 0;
        if (file != null) {
            coverId = fileManagerService.uploadPicture(file);
            cardEntity.setCoverId(coverId);
        }
        return cardMapper.updateByPrimaryKeySelective(cardEntity);
    }

    /**
     * 删除会员卡 （后端--会员卡类型）
     * gpd 2022-08-02 17:09
     * @param id
     * @return
     */
    @Override
    public Integer deleteCard(Integer id) {
        CardEntity cardEntity = new CardEntity();
        cardEntity.setId(id);
        cardEntity.setDelFlag(1);
        return cardMapper.updateByPrimaryKeySelective(cardEntity);
    }

    /**
     * 查询所有会员卡类型 （前端---会员卡中心）
     * gpd 2022-08-02 17:09
     * @return
     */
    @Override
    public List<CardEntity> getCards() {
        Example example = new Example(CardEntity.class);
        example.createCriteria().andEqualTo("delFlag", 0);
        return cardMapper.selectByExample(example);
    }

    /**
     * 查询已选择会员卡类型 （前端---选择会员卡页面）
     * gpd 2022-08-02 17:09
     * @param cardId
     * @return
     */
    @Override
    public List<CardEntity> getCardType(Integer cardId) {
        CardEntity cardEntity = new CardEntity();
        cardEntity.setId(cardId);
        return cardMapper.select(cardEntity);
    }


}
